//src/main/java/com/cuit/blog/controller/ArticleController.java
package com.cuit.blog.controller;
import com.cuit.blog.entity.Article;
import com.cuit.blog.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.List;
import org.springframework.ui.Model;import org.springframework.data.domain.Page;

@Controller
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    ArticleService articleService;

    private static final int pageSize = 10;

    @GetMapping("/findAll")
    public String findAll(Model model) {
        return findByPage(0, model);
    }

    @GetMapping("/page")
    public String findByPage(@RequestParam("pageIndex") int pageIndex, Model model) {
        Page<Article> articlePage = articleService.findAllByPage(pageIndex, pageSize);
        int pre = articlePage.previousOrFirstPageable().getPageNumber();
        int next = articlePage.nextOrLastPageable().getPageNumber();

        model.addAttribute("pre", pre);
        model.addAttribute("next", next);
        model.addAttribute("page", articlePage);
        model.addAttribute("articles", articlePage.getContent());

        return "article/article";
    }
    @GetMapping("/add")
    public String showAddForm(Model model) {
        Article article = new Article();
        model.addAttribute("article", article);
        return "article/add";
    }

    @PostMapping("/add")
    public String addArticle(@ModelAttribute Article article) {

        article.setPubDate(LocalDateTime.now().truncatedTo(ChronoUnit.SECONDS));

        articleService.save(article);
        return "redirect:/article/findAll";
    }
    @GetMapping("/delete/{id}")
    public String showDeleteConfirmation(@PathVariable int id, Model model) {
        Article article = articleService.findById(id);
        model.addAttribute("article", article);
        return "article/delete";
    }

    @PostMapping("/delete/{id}")
    public String deleteArticle(@PathVariable int id) {
        articleService.deleteById(id);
        return "redirect:/article/findAll";
    }

    @GetMapping("/edit/{id}")
    public String showEditForm(@PathVariable int id, Model model) {
        Article article = articleService.findById(id);
        model.addAttribute("article", article);
        return "article/edit";
    }

    @PostMapping("/edit/{id}")
    public String editArticle(@PathVariable int id, @ModelAttribute Article editedArticle) {

        editedArticle.setPubDate(LocalDateTime.now().truncatedTo(ChronoUnit.SECONDS));
        articleService.save(editedArticle);
        return "redirect:/article/findAll";
    }
    @GetMapping("/search")
    public String showSearchForm(Model model) {
        return "article/search";
    }

    @PostMapping("/searchResult")
    public String searchResult(@RequestParam int articleId, Model model) {
        Article article = articleService.findById((articleId));
        model.addAttribute("article", article);
        return "article/result";
    }
}